/*
 * @lc app=leetcode.cn id=20 lang=javascript
 *
 * [20] 有效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
  let stack = [];
  let Str = s.split('');
  if (Str[0] == ')' || Str[0] == ']' || Str[0] == '}') {
    return false;
  }
  for (let i = 0; i < Str.length; i++) {
    const ele = Str[i];
    switch (ele) {
      case '(':
        stack.push('(');
        break;
      case '[':
        stack.push('[');
        break;
      case '{':
        stack.push('{');
        break;
      case ')':
        if (stack[stack.length - 1] === '(') {
          stack.pop()
        } else {
          stack.push(')')
        }
        break;
      case ']':
        if (stack[stack.length - 1] === '[') {
          stack.pop()
        } else {
          stack.push(']')
        }
        break;
      case '}':
        if (stack[stack.length - 1] === '{') {
          stack.pop()
        } else {
          stack.push('}')
        }
        break;
      default:
        break;
    }
  }
  if (stack.length === 0) {
    return true;
  } else return false;

};
// @lc code=end